package com.im.server.enterprise.common.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import com.im.server.enterprise.common.bean.Department;
import com.im.server.enterprise.common.bean.DepartmentMember;
import com.im.server.enterprise.common.dao.DepartmentDAO;
import com.im.server.enterprise.common.dao.DepartmentMemberDAO;

/**
 * @author XiaHui
 * @date 2017年8月13日 上午10:00:16
 */
@Service
public class DepartmentMemberManager {

	protected final Logger logger = LogManager.getLogger(this.getClass());

	@Resource
	DepartmentMemberDAO departmentMemberDAO;
	@Resource
	DepartmentDAO departmentDAO;

	private Map<String, List<String>> map = new ConcurrentHashMap<String, List<String>>();

	
	public List<String> getDepartmentMemberIdList(String departmentId){
		List<String> list=map.get(departmentId);
		return list;
	}
	
	public void loadDepartmentMemberList() {
		synchronized (map) {
			map.clear();
			List<Department> departmentList = departmentDAO.getAllList();
			List<DepartmentMember> departmentMemberList = departmentMemberDAO.getAllList();

			Map<String, Set<String>> departmentMemberIdSetMap = new HashMap<String, Set<String>>();

			for (DepartmentMember dm : departmentMemberList) {
				String departmentId = dm.getDepartmentId();
				String userId = dm.getUserId();
				Set<String> memberIdSet = departmentMemberIdSetMap.get(departmentId);
				if (memberIdSet == null) {
					memberIdSet = new HashSet<String>();
					departmentMemberIdSetMap.put(departmentId, memberIdSet);
				}
				memberIdSet.add(userId);
			}

			Map<String, Set<Department>> nodeMap = new HashMap<String, Set<Department>>();

			Map<String, Department> departmentMap = new HashMap<String, Department>();
			for (Department d : departmentList) {
				departmentMap.put(d.getId(), d);
			}

			for (Department d : departmentList) {

				String superiorId = d.getSuperiorId();

				Department superior = departmentMap.get(superiorId);
				Set<Department> tempSet = null;
				while (null != superior) {

					Set<Department> nodeList = nodeMap.get(superiorId);
					if (nodeList == null) {
						nodeList = new HashSet<Department>();
						nodeMap.put(superiorId, nodeList);
					}
					nodeList.add(d);
					
					if (null != tempSet) {
						nodeList.addAll(tempSet);
					}

					tempSet = nodeList;
					d = superior;
					superiorId = superior.getSuperiorId();
					superior = departmentMap.get(superiorId);
				}
			}

			for (Department d : departmentList) {
				String id = d.getId();
				Set<String> memberIdSet = departmentMemberIdSetMap.get(id);
				if (memberIdSet == null) {
					memberIdSet = new HashSet<String>();
					departmentMemberIdSetMap.put(id, memberIdSet);
				}

				Set<Department> set = nodeMap.get(id);

				if (null != set) {
					for (Department n : set) {
						Set<String> nodeMemberIdSet = departmentMemberIdSetMap.get(n.getId());
						if (null != nodeMemberIdSet) {
							memberIdSet.addAll(nodeMemberIdSet);
						}
					}
				}
			}
			
			
			for (Department d : departmentList) {
				String id = d.getId();
				List<String> idList=new ArrayList<String>();
				Set<String> memberIdSet = departmentMemberIdSetMap.get(id);
				if (memberIdSet != null) {
					idList.addAll(memberIdSet);
				}
				map.put(id, idList);
			}
		}
	}
}